import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    id: '',
    real: '',
    createTime: '',
    updatedTime: '',
    openId: '',
    area: '',
    role: '',
    isHealth: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_REAL: (state, real) => {
    state.real = real
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_CREATE_TIME: (state, createTime) => {
    state.createTime = createTime
  },
  SET_UPDATED_TIME: (state, updatedTime) => {
    state.updatedTime = updatedTime
  },
  SET_OPEN_ID: (state, openId) => {
    state.openId = openId
  },
  SET_AREA: (state, area) => {
    state.area = area
  },
  SET_ROLE: (state, role) => {
    state.role = role
  },
  SET_IS_HEALTH: (state, isHealth) => {
    state.isHealth = isHealth
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        commit('SET_TOKEN', response.message)
        // 以下是request 需要的 token
        setToken(response.message)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  loginByCode({ commit }, token) {
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', token)
      // 以下是request 需要的 token
      setToken(token)
      console.log('设置token' + getToken())
      resolve()
    })
  },
  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      console.log('getUserInfo 的stateToken' + state.token)
      getInfo(state.token).then(response => {
        if (!response) {
          reject('Verification failed, please Login again.')
        }
        const dom = JSON.parse(response.message)
        const avatar = dom.avatar
        const name = dom.username
        const id = dom.id
        const role = dom.role
        const area = dom.area
        const createdTime = dom.createdTime
        const updatedTime = dom.updatedTime
        const real = dom.realName
        const openId = dom.openId
        const isHealth = dom.isHealth
        console.log({ avatar, name, id, role, area, createdTime, updatedTime, real, openId })
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_REAL', real)
        commit('SET_ID', id)
        commit('SET_CREATE_TIME', createdTime)
        commit('SET_UPDATED_TIME', updatedTime)
        commit('SET_OPEN_ID', openId)
        commit('SET_ROLE', role)
        commit('SET_AREA', area)
        commit('SET_IS_HEALTH', isHealth)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

